﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.ServiceModel;
using System.ServiceModel.Description;
using System.Text;
using HWmanagementDB;
using System.Runtime.Serialization;

namespace HWmanagementServer
{


    [ServiceContract]
    public class Manager
    {
        #region Admin manager functions

        [OperationContract]
        public void AddNewUser(UserModel um)
        {
            AdminManager.AddUser(um);
        }

        [OperationContract]
        public void AddNewSubject(SubjectModel sm)
        {
            AdminManager.AddSubject(sm);
        }

        [OperationContract]
        public void AddCourse(string subject,string semester)
        {
            AdminManager.AddCourse(subject, semester);
        }

        #endregion

        #region Auth and Guest manager functions

        [OperationContract]
        public bool LoggingInUser(UserModel um)
        {
            return PersonMgr.checkLogin(um.Neptun, um.Password);
        }

        [OperationContract]
        public UserModel GetLoggedInUser(string neptun)
        {
            return PersonMgr.getUserbyNeptun(neptun);
        }

        #endregion

        #region Common functions for students and teachers

        [OperationContract]
        public List<SubjectModel> getSubjects(string user)
        {
            return PersonMgr.getSubjects(user);
        }

        [OperationContract]
        public ResultModel viewResult(int hwid, string user)
        {
            return PersonMgr.viewResult(hwid, user);
        }

        #endregion

        #region Teacher functions

        [OperationContract]
        public List<FileModel> getFiles(int hwid, string user, bool onlyLast)
        {
            return TeacherMgr.getFiles(hwid, user, onlyLast);
        }

        [OperationContract]
        public List<HomeworkModel> getTeacherHomeworks(string user, int courseId)
        {
            return TeacherMgr.getTeacherHomeworks(user, courseId);
        }

        [OperationContract]
        public void postNewHomework(HomeworkModel hw, FileModel file)
        {
            TeacherMgr.postNewHomework(hw, file);
        }

        [OperationContract]
        public void writeResult(ResultModel res)
        {
            TeacherMgr.writeResult(res);
        }

        [OperationContract]
        public void addTeacher(string user, string subject)
        {
            TeacherMgr.addTeacher(user, subject);
        }

        [OperationContract]
        public void removeTeacher(string user, string subject)
        {
            TeacherMgr.removeTeacher(user, subject);
        }

        [OperationContract]
        public void modifyHomework(HomeworkModel hw)
        {
            TeacherMgr.modifyHomework(hw);
        }

        [OperationContract]
        public void modifyResult(ResultModel res)
        {
            TeacherMgr.modifyResult(res);
        }

        [OperationContract]
        public void deleteHomework(int hwid)
        {
            TeacherMgr.deleteHomework(hwid);
        }

        [OperationContract]
        public int getGroupOnHw(int hwId)
        {
            return TeacherMgr.getGroupOnHw(hwId);
        }

        #endregion

        #region Student functions

        public static List<UserModel> getStudentsOnSubject(int subjId)
        {
            return StudentMgr.getStudentsOnSubject(subjId);
        }

        [OperationContract]
        public List<UserModel> getUsersOnHw(int hwid)
        {
            return StudentMgr.getUsersOnHw(hwid);
        }

        [OperationContract]
        public List<HomeworkModel> getStudentHomeworks(string user, int courseId)
        {
            return StudentMgr.getStudentHomeworks(user, courseId);
        }

        [OperationContract]
        public void PostHomework(int hwid, string user, FileModel file)
        {
            StudentMgr.PostHomework(hwid, user, file);
        }

        [OperationContract]
        public void addStudentToGroup(string student, int group)
        {
            StudentMgr.addStudentToGroup(student, group);
        }

        [OperationContract]
        public void removeStudentFromGroup(string student, int group)
        {
            StudentMgr.removeStudentFromGroup(student, group);
        }

        #endregion

        #region Message handling functions

        [OperationContract]
        public void sendMessage(MessageModel msg)
        {
            MessageMgr.sendMessage(msg);
        }

        [OperationContract]
        public List<MessageModel> getMessages(string user)
        {
            return MessageMgr.getMessages(user);
        }

        [OperationContract]
        public void setRead(int msg_id, bool read)
        {
            MessageMgr.setRead(msg_id, read);
        }

        [OperationContract]
        public List<UserModel> getAllUser()
        {
            return MessageMgr.getAllUser();
        }

        #endregion

        #region Logging functions

        [OperationContract]
        public List<LogModel> getLogs()
        {
            return Program.log.getLogs();
        }

        #endregion

        #region Subject manipulation

        [OperationContract]
        public List<CourseModel> getCourses(string subject)
        {
            return SubjectMgr.getCourses(subject);
        }

        [OperationContract]
        public List<SubjectModel> getAllSubjects()
        {
            return SubjectMgr.getAllSubjects();
        }

        [OperationContract]
        public List<UserModel> getTeachers(string subjectId)
        {
            return SubjectMgr.getTeachers(subjectId);
        }

        #endregion

        #region File functions

        [OperationContract]
        public string getFileName(int fileId)
        {
            return FileMgr.getFileName(fileId);
        }

        #endregion
    }
    
    class Program
    {
        //the logger must be accessible form everywhere
        public static Logger log;

        static void Main(string[] args)
        {
            //adding the logger
            log = new Logger();


            Console.WriteLine("Service init...");
            log.LogToFile("Server side init.");

            var baseAddress = new Uri("http://localhost:8732/Design_Time_Addresses/hazi");
            using (ServiceHost host = new ServiceHost(typeof(Manager), baseAddress))
            {
                host.AddServiceEndpoint(typeof(Manager), new WSHttpBinding(), "Manager");

                ServiceMetadataBehavior smb = new ServiceMetadataBehavior();
                smb.HttpGetEnabled = true;
                smb.MetadataExporter.PolicyVersion = PolicyVersion.Policy15;
                host.Description.Behaviors.Add(smb);

                host.Open();
                log.LogToFile("Server side init done. Service up and running.");

                //Console.Clear();
                Console.WriteLine("Service initialized! \nService is running...");

                //for testing
                //TeacherMgr.addTeacher("NANANA", "VITMA234"); //OK
                //TeacherMgr.removeTeacher("NANANA", "VITMA234");   //OK
                /*MessageModel msg = new MessageModel();
                msg.is_read = false;
                msg.messageBody = "Teszt2";
                msg.receiver = "DEDEDE";
                msg.sender = "NANANA";
                msg.subject= "Teszt2";
                MessageMgr.sendMessage(msg);*/ //OK
                //List<MessageModel> lst = MessageMgr.getMessages("DEDEDE");    //OK
                //Console.WriteLine("msg: " + lst.ElementAt(0).messageBody);    



                //end of testing code

                Console.ReadKey();
                log.LogToFile("Server side service shutting down.");
            }
        }
    }
}
